package com.oasys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oasys.dao.EmployMapper;
import com.oasys.ecxeption.BusinessException;
import com.oasys.entity.EmployPo;
import com.oasys.entity.EmployVo;
import com.oasys.entity.LoginUser;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.EmployService;
import com.oasys.service.SysUserService;
import com.oasys.utils.FileUtils;
import com.oasys.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/14
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class EmployServiceImpl implements EmployService {

    @Resource
    private EmployMapper employMapper;

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询员工信息
     * @param employVo 姓名
     * @return res
     */
    @Override
    public ResponseResult<Map<String,Object>> getEmployInfo(EmployVo employVo) {
        Map<String,Object> map = new HashMap<>(8);
        try {
            PageHelper.startPage(employVo.getPage(),employVo.getSize());
            List<EmployPo> employInfo = employMapper.getEmployInfo(employVo.getName(), employVo.getSnick());
            if (ObjectUtils.isNotEmpty(employInfo)){
                for (EmployPo po : employInfo) {
                    getJobAndDeptInfo(po);
                }
            }
            PageInfo<EmployPo> pageInfo = new PageInfo<>(employInfo);
            map.put("employList",pageInfo.getList());
            map.put("totals",pageInfo.getTotal());
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * update
     * @param employPo
     * @return
     */
    @Override
    public ResponseResult<Boolean> updateStaffInfo(EmployPo employPo) {
        try {
            if (ObjectUtils.isEmpty(employPo)){
                return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,false);
            }
            Boolean result = employMapper.updateStaffInfo(employPo);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 离职
     * @param sid
     * @return
     */
    @Override
    public ResponseResult<Boolean> prohibitEmploy(String sid) {
        try {
            Boolean result = employMapper.prohibitEmploy(Long.parseLong(sid));
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 入职
     * @param sid
     * @return
     */
    @Override
    public ResponseResult<Boolean> inductionEmploy(String sid) {
        try {
            Boolean result = employMapper.inductionEmploy(Long.parseLong(sid));
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取个人信息
     * @return
     */
    @Override
    public ResponseResult<EmployPo> getSelfInfo() {
       try {
           log.info("获取个人信息");
           LoginUser loginUser = sysUserService.getLoginUser();
           EmployPo selfInfo = employMapper.getSelfInfo(loginUser.getUserId());
           getJobAndDeptInfo(selfInfo);
           return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,selfInfo);
       }catch (Exception e){
           e.printStackTrace();
       }
       return null;
    }

    /**
     * 获取员工职位、部门、状态 信息
     * @param selfInfo
     */
    private void getJobAndDeptInfo(EmployPo selfInfo) {
        selfInfo.setJobName(employMapper.getEmployJob(null == selfInfo.getJobId() ? null : selfInfo.getJobId()));
        selfInfo.setDeptName(employMapper.getEmployDept(null == selfInfo.getDeptId() ? null : selfInfo.getDeptId()));
        selfInfo.setStaffState(employMapper.getStaffState(null == selfInfo.getStateId() ? null : selfInfo.getStateId()));
    }

    /**
     * 根据userId 查询用户
     * @param userId
     * @return
     */
    public EmployPo findEmployInfoByUserId(Long userId){
        return employMapper.getSelfInfo(userId);
    }

    /**
     * 上传简历
     * @param multipartFile 简历文件
     * @return 简历地址
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @Override
    public String uploadResume(MultipartFile multipartFile) {
        // 获取当前登录的用户
        LoginUser loginUser = sysUserService.getLoginUser();
        // 识别文件类型
        String fileType = FileUtils.resolveFileType(multipartFile);
        // 创建文件名
        String fileName = FileUtils.createFileName(FileUtils.STORAGE_LOCATION_RESUME, fileType);
        // 数据库保存简历路径
        try {
            employMapper.removeResume(loginUser.getUserId());
            employMapper.saveResume(loginUser.getUserId(), fileName);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("上传简历失败");
        }
        // 保存简历文件
        FileUtils.saveFile(fileName, multipartFile);
        // 返回下载地址
        return FileUtils.createDownloadUri(fileName);
    }

    /**
     * 根据用户Id获取简历洗澡澡地址
     * @param userId 用户Id
     * @return 简历下载地址
     */
    @Override
    public String getResumeByUserId(Long userId) {
        if (userId == null) {
            userId = sysUserService.getLoginUser().getUserId();
        }
        return FileUtils.createDownloadUri(employMapper.getResume(userId));
    }
}
